#include "StdAfx.h"
#include "CustomVisualStyle.h"
#include "BCGPVisualManager2007.h"

IMPLEMENT_DYNCREATE(CustomVisualStyle, CBCGPVisualManager2010)

CustomVisualStyle::CustomVisualStyle(void)
{

}


CustomVisualStyle::~CustomVisualStyle(void)
{

}


// COLORREF CustomVisualStyle::GetTreeControlFillColor(CBCGPTreeCtrl* pTreeCtrl, BOOL bIsSelected, BOOL bIsFocused, BOOL bIsDisabled)
// {
// 	if (globalData.m_nBitsPerPixel <= 8 || globalData.IsHighContastMode ())
// 	{
// 		return CBCGPVisualManager2007::GetTreeControlFillColor(pTreeCtrl, bIsSelected, bIsFocused, bIsDisabled);
// 	}
// 
// 	if (bIsSelected)
// 	{
// 		return bIsFocused ? m_clrHighlightGradientDark : globalData.clrBarShadow;
// 	}
// 
// 	return (COLORREF)-1;
// }

void CustomVisualStyle::OnFillBarBackground  (CDC* pDC, CBCGPBaseControlBar* pBar,
	CRect rectClient, CRect rectClip,
	BOOL bNCArea)
{
	ASSERT_VALID (pDC);
	ASSERT_VALID (pBar);

	CBCGPDrawManager dm (*pDC);

	if (pBar->IsKindOf (RUNTIME_CLASS (CBCGPOutlookBarPane)))
	{
		//dm.FillGradient (rectClip, RGB (113, 175, 210), RGB (255, 255, 255), FALSE);
		dm.FillGradient (rectClip, RGB (221, 221, 221), RGB (221, 221, 221), FALSE);
		return;
	}

	if (!bNCArea)
	{
		CRgn rgn;
		rgn.CreateRectRgnIndirect (&rectClient);

		pDC->SelectClipRgn (&rgn);
	}

	CRect rectFill = rectClient;

	if (!pBar->IsFloating () &&
		!pBar->IsKindOf (RUNTIME_CLASS (CBCGPPopupMenuBar)))
	{
		CRect rectMainFrame;
		pBar->GetTopLevelFrame ()->GetWindowRect (rectMainFrame);

		pBar->ScreenToClient (&rectMainFrame);
		rectFill = rectMainFrame;

		if (bNCArea)
		{
			CRect rectWindow;
			pBar->GetWindowRect (rectWindow);

			pBar->ScreenToClient (rectWindow);

			CRect rectClientActual;
			pBar->GetClientRect (rectClientActual);

			rectFill.left += rectClientActual.left - rectWindow.left;
			rectFill.top += rectClientActual.top - rectWindow.top;
			rectFill.right += 10;
		}
	}

	//dm.FillGradient (rectFill, RGB (113, 175, 210), RGB (255, 255, 255), FALSE);
	dm.FillGradient (rectFill, RGB (221, 221, 221), RGB (221, 221, 221), FALSE);

	if (!bNCArea)
	{
		pDC->SelectClipRgn (NULL);
	}
}


COLORREF CustomVisualStyle::OnFillRibbonPanelCaption (
	CDC* /*pDC*/,
	CBCGPRibbonPanel* /*pPanel*/, 
	CRect /*rectCaption*/)
{
	if (!CanDrawImage ())
	{
		return globalData.clrCaptionText;
		//return RGB (221, 221, 221);
	}

	return m_clrRibbonPanelCaptionText;

	//return RGB (221, 221, 221);
}


/*
void CustomVisualStyle::OnDrawRibbonMainButton (
	CDC* pDC, 
	CBCGPRibbonButton* pButton)
{
	ASSERT_VALID (pDC);
	ASSERT_VALID (pButton);

	if (!CanDrawImage ())
	{
		CBCGPVisualManager2007::OnDrawRibbonMainButton(pDC, pButton);
		return;
	}

	BOOL bIsHighlighted = pButton->IsHighlighted () || pButton->IsFocused ();
	BOOL bIsPressed = pButton->IsPressed () || pButton->IsDroppedDown ();

	if (pButton->IsDroppedDown ())
	{
		bIsPressed = TRUE;
		bIsHighlighted = TRUE;
	}

	CRect rect = pButton->GetRect ();

	int index = 0;
	if (bIsPressed)
	{
		if (bIsHighlighted)
		{
			index = 2;
		}
	}
	else if (bIsHighlighted)
	{
		index = 1;
	}

	if (m_clrMainButton != (COLORREF)-1)
	{
		if (!m_ctrlRibbonBtnMainColorized.IsValid())
		{
			m_RibbonBtnMain.CopyTo(m_ctrlRibbonBtnMainColorized);
			m_ctrlRibbonBtnMainColorized.GetImages().AddaptColors(RGB(0, 0, 192), m_clrMainButton);
		}

		m_ctrlRibbonBtnMainColorized.Draw (pDC, pButton->GetRect (), index);
	}
	else
	{
		m_RibbonBtnMain.Draw (pDC, pButton->GetRect (), index);
	}
}
*/

/*
void CustomVisualStyle::OnDrawRibbonCaption (CDC* pDC, CBCGPRibbonBar* pBar,
	CRect rectCaption, CRect rectText)
{
	ASSERT_VALID (pDC);
	ASSERT_VALID (pBar);

	if (!CanDrawImage ())
	{
		CBCGPVisualManager2007::OnDrawRibbonCaption(pDC, pBar, rectCaption, rectText);
		return;
	}

	CWnd* pWnd = pBar->GetParent ();
	ASSERT_VALID (pWnd);

	const DWORD dwStyle   = pWnd->GetStyle ();
	const DWORD dwStyleEx = pWnd->GetExStyle ();

	const BOOL bIsRTL     = (dwStyleEx & WS_EX_LAYOUTRTL) == WS_EX_LAYOUTRTL;
	const BOOL bActive    = IsWindowActive (pWnd);
	const BOOL bGlass	  = pBar->IsTransparentCaption ();

	{
		CSize szSysBorder(globalUtils.GetSystemBorders (pBar->GetParent()));
		if (szSysBorder.cx == 0 && szSysBorder.cy == 0)
		{
			szSysBorder = globalUtils.GetSystemBorders (BCGCBProGetTopLevelFrame(pBar));
		}		

		if (!bGlass)
		{
			CRect rectCaption1(rectCaption);
			rectCaption1.InflateRect(szSysBorder.cx, szSysBorder.cy, szSysBorder.cx, 0);

			m_ctrlMainBorderCaption.Draw (pDC, rectCaption1, bActive ? 0 : 1);
		}

		CRect rectQAT = pBar->GetQuickAccessToolbarLocation ();

		if (rectQAT.left > rectQAT.right)
		{
			rectText.left = rectQAT.left + 1;
		}

		BOOL bHide  = (pBar->GetHideFlags () & BCGPRIBBONBAR_HIDE_ALL) != 0;
		BOOL bExtra = !bHide && pBar->IsQuickAccessToolbarOnTop () &&
			rectQAT.left < rectQAT.right && !pBar->IsQATEmpty();

		BOOL bDrawIcon = (bHide && !bExtra) || pBar->IsScenicLook ();

		if (bExtra)
		{
			CRect rectQAFrame (rectQAT);
			rectQAFrame.InflateRect (1, 1, 1, 1);

			const CBCGPControlRendererParams& params = m_ctrlRibbonCaptionQA.GetParams ();

			if (rectQAFrame.Height () < params.m_rectImage.Height ())
			{
				rectQAFrame.top = rectQAFrame.bottom - params.m_rectImage.Height ();
			}

			if (bGlass && pWnd->IsZoomed ())
			{
				m_ctrlRibbonCaptionQA_Glass.Draw (pDC, rectQAFrame, bActive ? 0 : 1);
			}

			rectQAFrame.right = pBar->GetQATCommandsLocation ().right + 1;
			m_ctrlRibbonCaptionQA.Draw (pDC, rectQAFrame, bActive ? 0 : 1);
		}

		if (bDrawIcon)
		{
			BOOL bDestroyIcon = FALSE;
			HICON hIcon = globalUtils.GetWndIcon (pWnd, &bDestroyIcon, FALSE);

			if (hIcon != NULL)
			{
				CSize szIcon (::GetSystemMetrics (SM_CXSMICON), ::GetSystemMetrics (SM_CYSMICON));

				long x = rectCaption.left + 2 + pBar->GetControlsSpacing().cx / 2;
				long y = rectCaption.top + max (0, (rectCaption.Height () - szIcon.cy) / 2);

				if (globalData.DwmIsCompositionEnabled () && IsDWMCaptionSupported())
				{
					y += 2;
				}

				if (bGlass)
				{
					globalData.DrawIconOnGlass (m_hThemeWindow, pDC, hIcon, CRect (x, y, x + szIcon.cx, y + szIcon.cy));
				}
				else
				{
					::DrawIconEx (pDC->GetSafeHdc (), x, y, hIcon, szIcon.cx, szIcon.cy,
						0, NULL, DI_NORMAL);
				}

				if (rectText.left < (x + szIcon.cx + 4))
				{
					rectText.left = x + szIcon.cx + 4;
				}

				if (bDestroyIcon)
				{
					::DestroyIcon (hIcon);
				}
			}
		}
	}

	CString strText;
	pWnd->GetWindowText (strText);

	CFont* pOldFont = (CFont*)pDC->SelectObject (&GetNcCaptionTextFont());
	ASSERT (pOldFont != NULL);

	CString strTitle (strText);
	CString strDocument;

	BOOL bPrefix = FALSE;
	if ((dwStyle & FWS_ADDTOTITLE) == FWS_ADDTOTITLE)
	{
		bPrefix = (dwStyle & FWS_PREFIXTITLE) == FWS_PREFIXTITLE;
		CFrameWnd* pFrameWnd = DYNAMIC_DOWNCAST(CFrameWnd, pWnd);

		if (pFrameWnd != NULL)
		{
			strTitle = pFrameWnd->GetTitle();

			if (!strTitle.IsEmpty ())
			{
				if (strText.GetLength () >= strTitle.GetLength ())
				{
					if (bPrefix)
					{
						int pos = strText.Find (strTitle, strText.GetLength () - strTitle.GetLength ());
						if (pos != -1)
						{
							strTitle = strText.Right (strTitle.GetLength () + 3);
							strDocument = strText.Left (strText.GetLength () - strTitle.GetLength ());
						}
					}
					else
					{
						int pos = strText.Find (strTitle);
						if (pos != -1)
						{
							strTitle = strText.Left (strTitle.GetLength () + 3);
							strDocument = strText.Right (strText.GetLength () - strTitle.GetLength ());
						}	
					}
				}
			}
			else
			{
				strDocument = strText;
			}
		}
	}

	DrawNcText (pDC, rectText, strTitle, strDocument, bPrefix, bActive, 
		bIsRTL, m_bNcTextCenter, bGlass, (pWnd->IsZoomed () && !globalData.bIsWindows7) ? 0 : 10, 
		(pWnd->IsZoomed () && !globalData.bIsWindows7) ? RGB (255, 255, 255) : (COLORREF)-1);

	pDC->SelectObject (pOldFont);
}
*/

COLORREF CustomVisualStyle::OnDrawRibbonCategoryCaption (
	CDC* pDC, 
	CBCGPRibbonContextCaption* pContextCaption)
{
	COLORREF clr = CBCGPVisualManager2007::OnDrawRibbonCategoryCaption (pDC, pContextCaption);

	if (CanDrawImage () && pContextCaption->GetColor () != BCGPCategoryColor_None)
	{
		XRibbonContextCategory2& context2 = 
			m_ctrlRibbonContextCategory2[pContextCaption->GetColor () - 1];

		CRect rect (pContextCaption->GetRect ());
		int xTabRight = pContextCaption->GetRightTabX ();

		if (xTabRight > 0 && pContextCaption->GetParentRibbonBar() != NULL && pContextCaption->GetParentRibbonBar ()->GetActiveCategory () != NULL)
		{
			CRect rectTab (pContextCaption->GetParentRibbonBar ()->GetActiveCategory ()->GetTabRect ());
			rect.top = rectTab.top;
			rect.bottom = rectTab.bottom;
			rect.right = xTabRight;

			context2.m_ctrlSeparator.Draw (pDC, rect);
		}
	}

	return clr;
}

void CustomVisualStyle::OnDrawRibbonCategory (
		CDC* pDC, 
		CBCGPRibbonCategory* pCategory,
		CRect rectCategory)
{
	if (CanDrawImage ())
	{
		if (pCategory->GetParentMenuBar () != NULL &&
			pCategory->GetParentMenuBar ()->IsKindOf (RUNTIME_CLASS(CBCGPRibbonBackstageView)))
		{
			return;
		}
	}

	CBCGPDrawManager dm (*pDC);
	dm.FillGradient (rectCategory, m_clrToolBarGradientDark, m_clrToolBarGradientLight, TRUE);
	return;

	
	
}
//*****************************************************************************
/*COLORREF CustomVisualStyle::OnFillRibbonButton(CDC* pDC, CBCGPRibbonButton* pButton)
{
	ASSERT_VALID (pDC);
	ASSERT_VALID (pButton);

	if (!CanDrawImage () || pButton->IsKindOf (RUNTIME_CLASS(CBCGPRibbonEdit)))
	{
		return CBCGPVisualManager2007::OnFillRibbonButton(pDC, pButton);
	}

	CRect rect (pButton->GetRect ());
	const BOOL bIsMenuMode = pButton->IsMenuMode ();

	CBCGPControlRenderer* pRenderer = NULL;
	CBCGPBitmapCache* pCache = NULL;
	int index = 0;

	BOOL bDisabled    = pButton->IsDisabled ();
	BOOL bWasDisabled = bDisabled;

	if (bDisabled && pButton->HasMenu ())
	{
		bDisabled = FALSE;
	}
	
	BOOL bFocused     = pButton->IsFocused ();
	BOOL bDroppedDown = pButton->IsDroppedDown ();
	BOOL bPressed     = pButton->IsPressed () && !bIsMenuMode;
	BOOL bChecked     = pButton->IsChecked ();
	BOOL bHighlighted = pButton->IsHighlighted () || bFocused;

	BOOL bDefaultPanelButton = pButton->IsDefaultPanelButton () && !pButton->IsQATMode () && !pButton->IsSearchResultMode ();

	if (pButton->IsBackstageViewMode () && !pButton->IsQATMode())
	{
		BOOL bDrawIcon = FALSE;
		CBCGPControlRenderer* pRenderer = &m_ctrlRibbonBtnBsNormal;
		if (m_clrMainButton != (COLORREF)-1)
		{
			if (!m_ctrlRibbonBtnBsNormalColorized.IsValid())
			{
				m_ctrlRibbonBtnBsNormal.CopyTo(m_ctrlRibbonBtnBsNormalColorized);
				m_ctrlRibbonBtnBsNormalColorized.GetImages().AddaptColors(RGB(0, 0, 192), m_clrMainButton);
			}

			pRenderer = &m_ctrlRibbonBtnBsNormalColorized;
		}

		int index = -1;
		COLORREF clr = m_clrRibbonBsTextNormal;

		if (bDisabled)
		{
			if (bFocused)
			{
				index = 2;
			}

			clr = m_clrRibbonBsTextDisabled;
		}
		else
		{
			if (bPressed)
			{
				index = 1;
				clr = m_clrRibbonBsTextActive;
			}
			else if (bFocused || bHighlighted)
			{
				index = 0;
				clr = m_clrRibbonBsTextHighlighted;
			}
		}

		if (pButton->GetBackstageAttachedView() != NULL)
		{
			pRenderer = &m_ctrlRibbonBtnBsView;

			if (m_clrMainButton != (COLORREF)-1)
			{
				if (!m_ctrlRibbonBtnBsViewColorized.IsValid())
				{
					m_ctrlRibbonBtnBsView.CopyTo(m_ctrlRibbonBtnBsViewColorized);
					m_ctrlRibbonBtnBsViewColorized.GetImages().AddaptColors(RGB(0, 0, 192), m_clrMainButton);
				}

				pRenderer = &m_ctrlRibbonBtnBsViewColorized;
			}

			if (!bDisabled && bChecked)
			{
				index = 1;
				bDrawIcon = TRUE;
				clr = m_clrRibbonBsTextActive;
			}
		}

		if (index != -1)
		{
			pRenderer->Draw (pDC, rect, index);
			if (bDrawIcon)
			{
				m_RibbonBtnBsViewIcon.DrawEx (pDC, rect, 0, CBCGPToolBarImages::ImageAlignHorzRight, CBCGPToolBarImages::ImageAlignVertCenter);
			}
		}

		return clr;
	}

	if (bFocused)
	{
		bDisabled = FALSE;
	}

	CBCGPBaseRibbonElement::RibbonElementLocation location = 
		pButton->GetLocationInGroup ();

	if (location != CBCGPBaseRibbonElement::RibbonElementNotInGroup &&
		pButton->IsShowGroupBorder ())
	{
		if (!pButton->GetMenuRect().IsRectEmpty ())
		{
			CRect rectC = pButton->GetCommandRect();
			CRect rectM = pButton->GetMenuRect();

			CBCGPControlRenderer* pRendererC = &m_ctrlRibbonBtnGroupMenu_F[0];
			CBCGPControlRenderer* pRendererM = &m_ctrlRibbonBtnGroupMenu_L[1];

			CBCGPBitmapCache* pCacheC = &m_cacheRibbonBtnGroupMenu_F[0];
			CBCGPBitmapCache* pCacheM = &m_cacheRibbonBtnGroupMenu_L[1];

			int indexC = 0;
			int indexM = 0;

			BOOL bHighlightedC = pButton->IsCommandAreaHighlighted ();
			BOOL bHighlightedM = pButton->IsMenuAreaHighlighted ();

			if (bChecked)
			{
				indexC = 3;
				indexM = 3;
			}

			if (bDisabled)
			{
				if (bChecked)
				{
					indexC = 5;
					indexM = 5;
				}
			}
			else
			{
				if (bDroppedDown && !bIsMenuMode)
				{
					indexC = 2;
					indexM = 3;
				}
				else
				{
					if (bFocused)
					{
						indexC = 6;
						indexM = 6;
					}

					if (bHighlightedC || bHighlightedM)
					{
						if (bChecked)
						{
							indexC = bHighlightedC ? 4 : 3;
							indexM = bHighlightedM ? 4 : 3;
						}
						else
						{
							indexC = bHighlightedC ? 1 : 6;
							indexM = bHighlightedM ? 1 : 6;
						}
					}

					if (bPressed)
					{
						if (bHighlightedC)
						{
							indexC = 2;
						}
					}
				}
			}

			if (indexC != -1 && indexM != -1)
			{
				int nCacheIndex = -1;
				if (pCacheC != NULL)
				{
					CSize size (rectC.Size ());
					nCacheIndex = pCacheC->FindIndex (size);
					if (nCacheIndex == -1)
					{
						nCacheIndex = pCacheC->Cache (size, *pRendererC);
					}
				}

				if (nCacheIndex != -1)
				{
					pCacheC->Get (nCacheIndex)->Draw (pDC, rectC, indexC);
				}
				else
				{
					pRendererC->Draw (pDC, rectC, indexC);
				}

				nCacheIndex = -1;
				if (pCacheM != NULL)
				{
					CSize size (rectM.Size ());
					nCacheIndex = pCacheM->FindIndex (size);
					if (nCacheIndex == -1)
					{
						nCacheIndex = pCacheM->Cache (size, *pRendererM);
					}
				}

				if (nCacheIndex != -1)
				{
					pCacheM->Get (nCacheIndex)->Draw (pDC, rectM, indexM);
				}
				else
				{
					pRendererM->Draw (pDC, rectM, indexM);
				}
			}

			return (COLORREF)-1;
		}
		else
		{
			if (bDroppedDown && !bIsMenuMode)
			{
				bChecked     = TRUE;
				bPressed     = FALSE;
				bHighlighted = FALSE;
			}

			pRenderer = &m_ctrlRibbonBtnGroup_S;
			pCache    = &m_cacheRibbonBtnGroup_S;

			if (bChecked)
			{
				index = 3;
			}

			if (bDisabled && !bFocused)
			{
				index = 0;
			}
			else
			{
				if (bPressed)
				{
					if (bHighlighted)
					{
						index = 2;
					}
				}
				else if (bHighlighted)
				{
					index++;
				}
			}
		}
	}
	else if (((!bDisabled && (bPressed || bChecked || bHighlighted || bDroppedDown)) || 
		      (bDisabled && bFocused)) && !bDefaultPanelButton)
	{
		if (!pButton->GetMenuRect().IsRectEmpty ())
		{
			CRect rectC = pButton->GetCommandRect();
			CRect rectM = pButton->GetMenuRect();

			CBCGPControlRenderer* pRendererC = pButton->IsMenuOnBottom () 
				? &m_ctrlRibbonBtnMenuV[0]
				: &m_ctrlRibbonBtnMenuH[0];
			CBCGPControlRenderer* pRendererM = pButton->IsMenuOnBottom () 
				? &m_ctrlRibbonBtnMenuV[1]
				: &m_ctrlRibbonBtnMenuH[1];

			int indexC = -1;
			int indexM = -1;

			BOOL bHighlightedC = pButton->IsCommandAreaHighlighted ();
			BOOL bHighlightedM = pButton->IsMenuAreaHighlighted ();

			if (bDisabled)
			{
				if (bHighlightedC || bHighlightedM)
				{
					indexC = 4;
					indexM = 4;

					if (bHighlightedM)
					{
						indexM = 0;

						if (bDroppedDown && !bIsMenuMode)
						{
							indexC = 5;
							indexM = 2;
						}
						else if (bPressed)
						{
							indexM = 1;
						}
					}
				}
			}
			else
			{
				if (bDroppedDown && !bIsMenuMode)
				{
					indexC = 1;
					indexM = 2;
				}
				else
				{
					if (bFocused)
					{
						indexC = 5;
						indexM = 5;
					}

					if (bChecked)
					{
						indexC = 2;
						indexM = 2;
					}

					if (bHighlightedC || bHighlightedM)
					{
						indexM = bChecked ? 2 : 5;

						if (bPressed)
						{
							if (bHighlightedC)
							{
								indexC = 1;
							}
							else if (bHighlightedM)
							{
								indexC = bChecked ? 2 : 5;
							}
						}
						else
						{
							if (bHighlightedC)
							{
								indexC = bChecked ? 3 : 0;
							}
							else if (bHighlightedM)
							{
								indexC = bChecked ? 2 : 5;
								indexM = bChecked ? 3 : 0;
							}
						}
					}
				}
			}

			if (indexC != -1)
			{
				pRendererC->Draw (pDC, rectC, indexC);
			}

			if (indexM != -1)
			{
				pRendererM->Draw (pDC, rectM, indexM);
			}

			return (COLORREF)-1;
		}
	}

	if (pRenderer != NULL)
	{
		COLORREF clrText = bWasDisabled
			? m_clrRibbonBarBtnTextDisabled
			: m_clrRibbonBarBtnText;
		
		if (!bWasDisabled && pButton->GetParentGroup () != NULL)
		{
			clrText = (COLORREF)-1;
		}

		if (index != -1)
		{
			int nCacheIndex = -1;
			if (pCache != NULL)
			{
				CSize size (rect.Size ());
				nCacheIndex = pCache->FindIndex (size);
				if (nCacheIndex == -1)
				{
					nCacheIndex = pCache->Cache (size, *pRenderer);
				}
			}

			if (nCacheIndex != -1)
			{
				pCache->Get (nCacheIndex)->Draw (pDC, rect, index);
			}
			else
			{
				pRenderer->Draw (pDC, rect, index);
			}

			if (!bWasDisabled)
			{
				clrText = m_clrRibbonBarBtnTextHighlighted;
			}
		}

		return clrText;
	}

	return CBCGPVisualManager2007::OnFillRibbonButton(pDC, pButton);
}
*/
COLORREF CustomVisualStyle::OnDrawRibbonCategoryTab (
					CDC* pDC, 
					CBCGPRibbonTab* pTab,
					BOOL bIsActive)
{
	ASSERT_VALID (pDC);
	ASSERT_VALID (pTab);

	if (!CanDrawImage ())
	{
		return CBCGPVisualManager2007::OnDrawRibbonCategoryTab (pDC, 
											pTab, bIsActive);
	}

	CBCGPRibbonCategory* pCategory = pTab->GetParentCategory ();
	ASSERT_VALID (pCategory);
	CBCGPRibbonBar* pBar = pCategory->GetParentRibbonBar ();
	ASSERT_VALID (pBar);

	bIsActive = bIsActive && 
		((pBar->GetHideFlags () & BCGPRIBBONBAR_HIDE_ELEMENTS) == 0 || pTab->GetDroppedDown () != NULL);

	const BOOL bPressed     = pTab->IsPressed ();
	const BOOL bIsFocused	= pTab->IsFocused () && (pBar->GetHideFlags () & BCGPRIBBONBAR_HIDE_ELEMENTS);
	const BOOL bIsHighlight = (pTab->IsHighlighted () || bIsFocused) && !pTab->IsDroppedDown ();

	CRect rectTab (pTab->GetRect ());
	rectTab.bottom++;

	int ratio = 0;
	if (m_ctrlRibbonCategoryTabSep.IsValid ())
	{
		ratio = pBar->GetTabTrancateRatio ();
	}

	if (ratio > 0)
	{
		rectTab.left++;
	}

	CBCGPControlRenderer* pRenderer = &m_ctrlRibbonCategoryTab;
	CBCGPControlRenderer* pRendererGlow = NULL;
	COLORREF clrText = m_clrRibbonCategoryText;
	COLORREF clrTextHighlighted = m_clrRibbonCategoryTextHighlighted;
	COLORREF clrTextActive = m_clrRibbonCategoryTextActive;

	if (pCategory->GetTabColor () != BCGPCategoryColor_None)
	{
		XRibbonContextCategory& context = 
				m_ctrlRibbonContextCategory [pCategory->GetTabColor () - 1];

		pRenderer = &context.m_ctrlTab;
		clrText   = context.m_clrText;
		clrTextHighlighted = context.m_clrTextHighlighted;
		clrTextActive = context.m_clrTextActive;

		if (bIsActive && (pBar->GetHideFlags () & BCGPRIBBONBAR_HIDE_ELEMENTS) == 0)
		{
			XRibbonContextCategory2& context2 = 
				m_ctrlRibbonContextCategory2[pCategory->GetTabColor () - 1];
			if (context2.m_ctrlTabGlow.IsValid ())
			{
				pRendererGlow = &context2.m_ctrlTabGlow;
			}
		}
	}

	if (bIsActive || bPressed || bIsHighlight)
	{
		if (pRendererGlow != NULL)
		{
			CRect rectImage (pRendererGlow->GetParams ().m_rectImage);
			CRect rectActive (rectTab);
			if (rectActive.Width () > rectImage.Width ())
			{
				rectActive.left += (rectActive.Width () - rectImage.Width ()) / 2;
				rectActive.right = rectActive.left + rectImage.Width ();
			}

			rectActive.OffsetRect (0, -rectImage.Height () / 2);
			rectActive.bottom = rectActive.top + rectImage.Height ();
			pRendererGlow->Draw (pDC, rectActive);
		}

		int nImage = 0;

		if (bIsActive)
		{
			nImage = 1;

			if (pBar->GetKeyboardNavigationLevel () >= 0)
			{
				nImage = 2;
			}
		}

		pRenderer->Draw (pDC, rectTab, nImage);
	}

	if (ratio > 0)
	{
		CRect rectSep (rectTab);
		rectSep.left = rectSep.right;
		rectSep.right += m_ctrlRibbonCategoryTabSep.GetParams ().m_rectImage.Width ();
		rectSep.bottom--;

		m_ctrlRibbonCategoryTabSep.Draw (pDC, rectSep, 0, (BYTE)min(ratio * 255 / 100, 255));
	}

	if (clrTextActive == (COLORREF)-1)
	{
		return bIsActive
				? clrTextHighlighted
				: clrText;
	}

	return bIsActive
			? clrTextActive
			: bIsHighlight
				? clrTextHighlighted
				: clrText;
}
//*****************************************************************************
COLORREF CustomVisualStyle::OnDrawRibbonPanel (
		CDC* pDC,
		CBCGPRibbonPanel* pPanel, 
		CRect rectPanel,
		CRect rectCaption)
{
	if (!CanDrawImage ())
	{
		return CBCGPVisualManager2007::OnDrawRibbonPanel (pDC, pPanel, rectPanel, rectCaption);
	}

	ASSERT_VALID (pDC);
	ASSERT_VALID (pPanel);

	COLORREF clrText = m_clrRibbonPanelText;
	
	if (pPanel->IsKindOf (RUNTIME_CLASS(CBCGPRibbonMainPanel)))
	{
		if (!pPanel->IsBackstageView())
		{
			const int nBorderSize = GetPopupMenuBorderSize ();
			rectPanel.InflateRect (nBorderSize, nBorderSize);
			
			m_ctrlRibbonMainPanel.Draw (pDC, rectPanel);
		}
		else
		{
			rectPanel = ((CBCGPRibbonMainPanel*)pPanel)->GetCommandsFrame ();
			m_ctrlRibbonBsPanelBack.Draw (pDC, rectPanel);
		}
	}
	else
	{
		BOOL bHighlighted = pPanel->IsHighlighted ();

		if (bHighlighted)
		{
			clrText = m_clrRibbonPanelTextHighlighted;
		}

		CBCGPRibbonPanelMenuBar* pMenuBar = pPanel->GetParentMenuBar ();
		if (!pPanel->IsMenuMode () && !pPanel->IsCollapsed () && (pMenuBar == NULL || pMenuBar->GetCategory () != NULL))
		{
			CBCGPControlRenderer* pRenderer = &m_ctrlRibbonPanelBack;
			CBCGPBitmapCache* pCache = &m_cacheRibbonPanelBack;

			CBCGPRibbonCategory* pCategory = pPanel->GetParentCategory ();
			ASSERT_VALID (pCategory);

			CBCGPBaseRibbonElement* pParentButton = pPanel->GetParentButton ();

			if (pCategory->GetTabColor () != BCGPCategoryColor_None &&
				(pParentButton == NULL || !pParentButton->IsQATMode ()))
			{
				XRibbonContextCategory2& context2 = 
					m_ctrlRibbonContextCategory2[pCategory->GetTabColor () - 1];

				if (context2.m_ctrlPanelBack.IsValid ())
				{
					pRenderer = &context2.m_ctrlPanelBack;
					pCache    = &context2.m_cachePanelBack;
				}

				clrText = bHighlighted 
							? m_clrRibbonContextPanelTextHighlighted
							: m_clrRibbonContextPanelText;
			}

			const CBCGPControlRendererParams& params = pRenderer->GetParams ();

			int nCacheIndex = -1;
			CSize size (params.m_rectImage.Width (), rectPanel.Height ());
			nCacheIndex = pCache->FindIndex (size);
			if (nCacheIndex == -1)
			{
				nCacheIndex = pCache->CacheY (size.cy, *pRenderer);
			}

			if (pPanel->IsOnDialogBar())
			{
				pRenderer->FillInterior (pDC, rectPanel, bHighlighted ? 1 : 0);

				CRect rectSeparator = rectPanel;
				rectSeparator.top = rectSeparator.bottom - 2;

				DrawSeparator (pDC, rectSeparator, m_penSeparatorDark, m_penSeparator2, TRUE);
			}
			else
			{
				if (nCacheIndex != -1)
				{
					pCache->Get(nCacheIndex)->DrawY (pDC, rectPanel, 
						CSize (params.m_rectInter.left, params.m_rectImage.right - params.m_rectInter.right), bHighlighted ? 1 : 0);
				}
				else
				{
					pRenderer->Draw (pDC, rectPanel, bHighlighted ? 1 : 0);
				}
			}
		}
	}
	
	return clrText;
}


void CustomVisualStyle::OnPreDrawRibbon (CDC* pDC, CBCGPRibbonBar* pRibbonBar, CRect rectTabs)
{
	ASSERT_VALID(pDC);
	ASSERT_VALID(pRibbonBar);

	if (!CanDrawImage ())
	{
		CBCGPVisualManager2007::OnPreDrawRibbon (pDC, pRibbonBar, rectTabs);
		return;
	}

	if (pRibbonBar->AreTransparentTabs())
	{
		rectTabs.top = rectTabs.bottom - m_ctrlRibbonCategoryTabFrame.GetParams ().m_rectImage.Height ();
		m_ctrlRibbonCategoryTabFrame.Draw (pDC, rectTabs);
	}
	else
	{
		if (pRibbonBar->GetParent()->GetSafeHwnd() != NULL && !IsWindowActive(pRibbonBar->GetParent()))
		{
			m_ctrlMainBorderCaption.FillInterior(pDC, rectTabs, 1);
		}
	}
}